import { observable, action } from "mobx";
import resource from "@/utils/resource";
import API from "@/utils/monitorwarning";
class SubjectStore {
    @observable resultData = [{ id: 1, name: "重庆渝北区团贷", people: 12, APP: 20 }];
    @observable curTab = "people";
    @observable status = {};
    @observable curItem = "";
    @observable curItemId = "";

    @observable subjectChildList = [];

    @observable selectedUsers = [];

    @action.bound
    getSubjectList(params) {
        resource.get(API.monitorwarningSubjectList, params).then(res => {
            this.resultData = res.list;
            this.curItem = this.resultData[0];
        });
    }

    @action.bound
    getSubjectAppList(id) {
        resource
            .get(API.monitorwarningSubjectPeopleList, {
                id: id || this.curItemId || this.curItem.id
            })
            .then(res => {
                this.selectedUsers = res.list.map(item => {
                    if (item.checked) return item.id;
                });
                this.subjectChildList = res.list;
            });
    }

    @action.bound
    selectUser = id => {
        const num = this.selectedUsers.indexOf(id);
        if (num > -1) {
            // this.selectedUsers = ;
            this.selectedUsers.splice(num, 1);
        } else {
            this.selectedUsers = [...this.selectedUsers, id];
        }
    };

    @action.bound
    changeTab = tab => {
        this.curTab = tab;
        this.getSubjectAppList();
    };

    @action.bound
    selectCurItem = tab => {
        this.curItem = tab;
        this.curItemId = tab.id;
    };

    @action.bound
    selectCurItemId = id => {
        this.curItemId = id;
    };

    @action.bound
    sendShare = () => {
        console.log("分享操作");
    };

    @action.bound
    updateStatus = newStatus => {
        this.status = {
            ...this.status,
            ...newStatus
        };
    };

    @action.bound
    changeEditStatus = id => {
        this.resultData = this.resultData.map(item => {
            if (item.id === id) item.edit = true;
            return item;
        });
    };

    @action.bound
    changeResultData = (id, value) => {
        this.resultData = this.resultData.map(item => {
            if (item.id === id) item.name = value;
            return item;
        });
    };

    @action.bound
    saveData = (id, value) => {
        resource.get(API.monitorwarningSubjectSave, { id, name: value }).then(res => {
            console.log("保存成功！", res.message);
        });
    };

    // 提交备注
    @action.bound
    submitText = value => {
        console.log(value, this.curItem.id);
    };
}

export default new SubjectStore();
